if isVoiceEnabled() then
	addEventHandler ( "onPlayerQuit", root, 
		function()
			local previousChannel = playerChannels[source]
			--Remove them from any previous channels
			if tonumber(previousChannel) then
				channels[previousChannel][source] = nil
				--Delete the empty table if he was the last player
				if not next(channels[previousChannel]) then
					channels[previousChannel] = nil
				end
			end
			playerChannels[source] = nil
		end
	)

	function getPlayerChannel ( player )
		if not checkValidPlayer ( player ) then return false end
		return playerChannels[player]
	end

	function setPlayerChannel ( player, id )
		if not checkValidPlayer ( player ) then return false end
		id = tonumber(id)
		if not id then
			return setPlayerDefaultChannel ( player )
		end
		local previousChannel = playerChannels[player]
		--Remove them from any previous channels
		if tonumber(previousChannel) then
			channels[previousChannel][player] = nil
			--Delete the empty table if he was the last player
			if not next(channels[previousChannel]) then
				channels[previousChannel] = nil
			end
		end
		playerChannels[player] = id
		--Insert them into the new channel
		channels[id] = channels[id] or {}
		channels[id][player] = true
		--Update all players in this channel of the new player in this channel
		playersInChannel = getPlayersInChannel ( id )
		for i,v in ipairs(playersInChannel) do
			setPlayerVoiceBroadcastTo ( v, playersInChannel )
		end
		return true
	end

	function getPlayersInChannel ( id )
		if not isElement(id) then
			id = tonumber(id)
			if not id then
				outputDebugString ( "getPlayersInChannel: Bad 'id' argument", 2 )
				return false
			end
		end
		return tableToArray(channels[id] or {})
	end

	function getNextEmptyChannel()
		local emptyChannel = 1
		while channels[emptyChannel] do
			emptyChannel = emptyChannel + 1
		end
		return emptyChannel
	end

else
	getPlayerChannel = outputVoiceNotLoaded
	setPlayerChannel = outputVoiceNotLoaded
	getPlayersInChannel = outputVoiceNotLoaded
	getNextEmptyChannel = outputVoiceNotLoaded
end

function join ()
    setPlayerChannel(source,0)
    setElementData(source,"player:kanal",0)
end
addEventHandler ( "onPlayerJoin", getRootElement(), join )

addCommandHandler("kanal", function(plr,cmd,kanal)
		if not tonumber(kanal) then
			outputChatBox("Użycie: /kanal <ID>",plr,255,255,255,true)
			return
		end
		if tonumber(kanal) < 1 or tonumber(kanal) > 6 then return end
		if tonumber(kanal) == 2 and not getElementData(plr,"player:duty") then outputChatBox("#ff0000[VoiceChat]#F0E68C Ten kanał jest dostępny tylko dla frakcji.",plr,255,255,255,true) return end
		if tonumber(kanal) == 3 and getElementData(plr,"player:duty") ~= "SAPD" then  outputChatBox("#ff0000[VoiceChat]#F0E68C Ten kanał jest dostępny tylko dla policji.",plr,255,255,255,true) return end
		if tonumber(kanal) == 4 and getElementData(plr,"player:duty") ~= "PSP" then  outputChatBox("#ff0000[VoiceChat]#F0E68C Ten kanał jest dostępny tylko dla straży pożarnej.",plr,255,255,255,true) return end
		if tonumber(kanal) == 5 and getElementData(plr,"player:duty") ~= "ŻW" then  outputChatBox("#ff0000[VoiceChat]#F0E68C Ten kanał jest dostępny tylko dla żandarmerii wojskowej.",plr,255,255,255,true) return end
		if tonumber(kanal) == 6 and getElementData(plr,"player:duty") ~= "PD" then  outputChatBox("#ff0000[VoiceChat]#F0E68C Ten kanał jest dostępny tylko dla pomocy drogowej.",plr,255,255,255,true) return end
		setPlayerChannel(plr,kanal)
		setElementData(plr,"player:kanal",kanal)
		outputChatBox("#00ff00[VoiceChat]#F0E68C Pomyślnie zmieniłeś kanał głosowy na #ccc000"..kanal.."",plr,255,255,255,true)
end)

addCommandHandler("graczekanal", function(plr,cmd,kanal)
		if not tonumber(kanal) then
			outputChatBox("Użycie: /kanal <ID>",plr,255,255,255,true)
			return
		end
		gracze = getPlayersInChannel ( kanal )
		if #gracze < 1 then outputChatBox("#ff0000 ---- #808080Brak dostępnych graczy na kanale #ccc000"..kanal.." #ff0000----",plr,255,0,0,true) return end
		outputChatBox("#ff0000 ---- #808080Gracze dostępni na kanale #ccc000"..kanal.." #ff0000----",plr,255,0,0,true)
		for i,v in ipairs(gracze) do
			outputChatBox(""..getPlayerName(v):gsub("#%x%x%x%x%x%x","").." ["..getElementData(v,"id").."]",plr,255,255,255,true)
		end
end)

addCommandHandler("kanaly", function(plr,cmd)
		outputChatBox("#ff0000 ---- #ccc000Spis kanałów głosowych na serwerze #ff0000----",plr,255,0,0,true)
		outputChatBox("#F0E68CKanał 1 #ffffff- Ogólny",plr,0,0,0,true)
		outputChatBox("#F0E68CKanał 2 #ffffff- #D2691EFrakcyjny",plr,0,0,0,true)
		outputChatBox("#F0E68CKanał 3 #ffffff- #00FFFFSłużba Policja",plr,0,0,0,true)
		outputChatBox("#F0E68CKanał 4 #ffffff- #800000Służba Straż Pożarna",plr,0,0,0,true)
		outputChatBox("#F0E68CKanał 5 #ffffff- #008000Służba Żandarmeria Wojskowa",plr,0,0,0,true)
		outputChatBox("#F0E68CKanał 6 #ffffff- #fe7f00Służba Pomoc Drogowa",plr,0,0,0,true)
end)




